package org.example;

public class Test2 {
    //leetcode 13 罗马数字转整数 https://leetcode.cn/problems/roman-to-integer/description/?envType=study-plan-v2&envId=top-interview-150
    public int romanToInt(String s) {
        int[] arr = new int[22];
        arr['I'-67] = 1;
        arr['V'-67] = 5;
        arr['X'-67] = 10;
        arr['L'-67] = 50;
        arr['C'-67] = 100;
        arr['D'-67] = 500;
        arr['M'-67] = 1000;
        int ret = 0;
        for (int i = 0; i < s.length(); i++) {
            if (i + 1 < s.length() && arr[s.charAt(i)-67] < arr[s.charAt(i+1)-67]) {
                ret -= arr[s.charAt(i)-67];
            }else {
                ret += arr[s.charAt(i)-67];
            }
        }
        return ret;
    }
}
